import asyncio, time


async def my_task(m):
    await asyncio.sleep(2)
    return f"Task completed :{m}"


async def main():
    coroutine20 = my_task(20)
    coroutine40 = my_task(40)
    coroutine60 = my_task(60)
    coroutine80 = my_task(80)
    tasks = [
        asyncio.create_task(coroutine20),
        asyncio.create_task(coroutine40),
        asyncio.create_task(coroutine60),
        asyncio.create_task(coroutine80),
    ]

    # await asyncio.wait(tasks）返回的是任务
    # dones, _ = await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
    # for task in dones:
    #     print(task.result())

    # await asyncio.gather(*tasks)返回的是任务的返回值
    result = await asyncio.gather(*tasks)
    for task in result:
        print(task)


if __name__ == "__main__":
    start = time.time()
    try:
        # 直接调用 asyncio.run 并传入 main 协程
        asyncio.run(main())
    except Exception as e:
        # 捕获由 asyncio.run 抛出的异常
        print(f"An exception occurred: ")

    # 打印总运行时间
    print(f"Time is :{time.time() - start}")
